package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type DeviceMapperEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	ObjectName   string // 设备厂商名称
	ValueType    string // 设备类型名
	MapperName   string // 映射名称
	MapperMode   int    // 映射模式

	ExtendParam map[string]interface{} // 扩展参数
}

type deviceMapperPo struct {
	Id           int64                  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`    // 设备类型名
	ObjectName   string                 `json:"objectName" db:"object_name"`    // 设备名称
	ValueType    string                 `json:"valueType" db:"value_type"`      // 通道类型
	MapperName   string                 `json:"mapperName" db:"mapper_name"`    // 映射名称
	MapperMode   int                    `json:"mapperMode" db:"mapper_mode"`    // 映射模式
	ExtendParam  map[string]interface{} `json:"extendParam" db:"extend_param"`  // 扩展参数
}

func (e *DeviceMapperEntity) Type() string {
	return "DeviceMapperEntity"
}

func (e *DeviceMapperEntity) TableName() string {
	return "tb_device_mapper"
}

func (e *DeviceMapperEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *DeviceMapperEntity) Instance() *DeviceMapperEntity {
	return &DeviceMapperEntity{
		BaseEntity:  new(commEntity.BaseEntity),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *DeviceMapperEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *DeviceMapperEntity) NewPo() interface{} {
	return &deviceMapperPo{
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *DeviceMapperEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *DeviceMapperEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Manufacturer)
	p = append(p, e.DeviceType)
	p = append(p, e.ObjectName)
	return p
}

func (e *DeviceMapperEntity) BuildByJson(jsn *string) error {
	var po deviceMapperPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *DeviceMapperEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *DeviceMapperEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*deviceMapperPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.ValueType = po.ValueType
	ptr.ObjectName = po.ObjectName
	ptr.MapperName = po.MapperName
	ptr.MapperMode = po.MapperMode
	ptr.ExtendParam = po.ExtendParam
}

func (e *DeviceMapperEntity) BuildToPo() interface{} {
	ptr := e
	po := deviceMapperPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.ValueType = ptr.ValueType
	po.ObjectName = ptr.ObjectName
	po.MapperName = ptr.MapperName
	po.MapperMode = ptr.MapperMode
	po.ExtendParam = ptr.ExtendParam

	return &po
}

func (e *DeviceMapperEntity) BindEntity(other *DeviceObjInfEntity) {
	ptr := e

	ptr.Manufacturer = other.Manufacturer
	ptr.DeviceType = other.DeviceType
	ptr.ObjectName = other.ObjectName
	ptr.ValueType = other.ValueType

	ptr.MapperName = other.ObjectName
	ptr.MapperMode = 0

	ptr.BaseEntity.Id = other.BaseEntity.Id
	ptr.BaseEntity.CreateTime = other.BaseEntity.CreateTime
	ptr.BaseEntity.UpdateTime = other.BaseEntity.UpdateTime
}
